setwd("~/Desktop/gitmo/playpen/r") # Your directory here
Get cb_2014_us_state_5m.zip and unzip it into the data folder in your path1 This contains the cartographic boundary files.
library(rgdal)
setwd("~/Desktop/gitmo/playpen/r") # your directory here
dsn <- "data/cb_2014_us_state_5m"
layer <- "cb_2014_us_state_5m"
cb5 = readOGR(dsn, layer)
## OGR data source with driver: ESRI Shapefile
## Source: "data/cb_2014_us_state_5m", layer: "cb_2014_us_state_5m"
## with 56 features
## It has 9 fields
#save(cb5, file ="data/cb5.Rda") # uncomment first use
setwd("~/Desktop/gitmo/playpen/r") # your directory here
download.file("https://gist.githubusercontent.com/technocrat/7719f51d0ae0e42edeb2/raw/f4c3157f17b3822416637a987d57bba678cb7839/popBase.R", "popBase.R", method = "libcurl")
source("R/popBase.R")
setwd("~/Desktop/gitmo/playpen/r") # your directory here
sources <- c("Source: U.S. Census Bureau 2014 Population Estimates http://1.usa.gov/1LPM7hc\n and Cartographic Boundary File (States, cb_2014_us_state_5m) http://1.usa.gov/1IZgFLV\nPrepared by: @technocrat", pub_date)
b = ggplot(data= map) +
geom_map(map=map, aes(x=long, y=lat, map_id=id, group=group),fill="white", color="black", size=0.3) +
no_ylab +
no_xlab +
plain_theme
n = b + geom_polygon(data = map, aes(x=long, y=lat, group=group, fill=pop.natural), color = "dark grey", size = 0.3)
my_title=""
unique(pop$pop.equal)
## [1] [5.84e+05,6.95e+06] (3.24e+07,3.88e+07] (1.97e+07,2.61e+07]
## [4] (6.95e+06,1.33e+07] (2.61e+07,3.24e+07]
## 6 Levels: [5.84e+05,6.95e+06] (6.95e+06,1.33e+07] ... (3.24e+07,3.88e+07]
n = n + scale_fill_discrete(name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title)
l = geom_text(data=centroids, aes(clong, clat, label = id), color = "black", size=2)
makewhite = centroids[c('CA'),]
Additional states can be set by extending the list
makewhites = centroids[c('CA','TX'),]
w = geom_text(data=makewhite, aes(clong, clat, label = id), color = "white", size=2)
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, default colors"
n = n + scale_fill_discrete(name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title)
n + l # use this order
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, tonal default"
n + scale_fill_grey(name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, tonal default, reversed order"
n + scale_fill_grey(start = 0.8, end = 0.2, na.value = "grey50",name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l + w
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed first half of color wheel"
n + scale_fill_discrete(h = c(0, 180) + 15, c = 100, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed first half of color wheel, reversed"
n + scale_fill_discrete(h = c(0, 180) + 15, c = 100, l = 65, h.start = 0, direction = -1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second half of color wheel"
n + scale_fill_discrete(h = c(180, 360) + 15, c = 100, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second half of color wheel, reversed"
n + scale_fill_discrete(h = c(180, 360) + 15, c = 100, l = 65, h.start = 0, direction = -1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second half of color wheel, reduced chroma"
n + scale_fill_discrete(h = c(180, 360) + 15, c = 60, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second half of color wheel,\nreduced chroma, subdued luminance"
n + scale_fill_discrete(h = c(180, 360) + 15, c = 60, l = 45, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second and third quarters of color wheel"
n + scale_fill_discrete(h = c(90, 270) + 15, c = 100, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, color default, compressed, second and third quarters of color wheel, reversed"
n + scale_fill_discrete(h = c(90, 270) + 15, c = 100, l = 65, h.start = 0, direction = -1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, fourth and first quarters of color wheel"
n + scale_fill_discrete(h = c(270, 90) + 15, c = 100, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, fourth and first quarters of color wheel"
n + scale_fill_discrete(h = c(270, 90) + 15, c = 100, l = 65, h.start = 0, direction = -1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte YlOrRd"
fire = "YlOrRd"
n + scale_fill_brewer(palette = fire, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte YlOrBr"
drought = "YlOrBr"
n + scale_fill_brewer(palette = drought, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte YlGnBu"
spring = "YlGnBu"
n + scale_fill_brewer(palette = spring, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
reds = "Reds"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Reds"
n + scale_fill_brewer(palette = reds, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
mauve = "RdPu"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte RdPu"
n + scale_fill_brewer(palette = mauve, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
purples = "Purples"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Purples"
n + scale_fill_brewer(palette = purples, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
lavender = "PuRd"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte PuRd"
n + scale_fill_brewer(palette = lavender, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
purplehaze = "PuBuGn"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte PuGuGn"
n + scale_fill_brewer(palette = purplehaze, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
greenhaze = "PuBuGn"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte PuGuGn"
n + scale_fill_brewer(palette = greenhaze, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
violets = "PuBu"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte PuBu"
n + scale_fill_brewer(palette = violets, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
sunset = "OrRd"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte OrRd"
n + scale_fill_brewer(palette = sunset, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
oranges = "Oranges"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Oranges"
n + scale_fill_brewer(palette = oranges, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
greys = "Greys"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Greys"
n + scale_fill_brewer(palette = greys, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
greens = "Greens"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Greens"
n + scale_fill_brewer(palette = greens, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
greenblue = "GnBu"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte GnBu"
n + scale_fill_brewer(palette = greenblue, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
twilight = "BuPu"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte BuPu"
n + scale_fill_brewer(palette = twilight, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
bluegreen = "BuGn"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte BuGn"
n + scale_fill_brewer(palette = bluegreen, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
blues = "Blues"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Blues"
n + scale_fill_brewer(palette = blues, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
# Custom palettes
# install.packages("wesanderson") # install to see this case
library(wesanderson)
#rushmore = wes_palette("Rushmore")
#my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, wesanderson palatte Rushmore"
#n + scale_fill_manual(values = rushmore, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
wes_palette("Rushmore")
diy = c("#FF0000", "#0000FF", "#00FF00","#d6a40c","#800080","#b2151a")
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, do it yourself palatte"
n + scale_fill_manual(values = diy, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l
sessionInfo()
## R version 3.2.1 (2015-06-18)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.10.4 (Yosemite)
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] wesanderson_0.3.2 RCurl_1.95-4.7 bitops_1.0-6
## [4] rgeos_0.3-11 mapproj_1.2-3 maps_2.3-10
## [7] maptools_0.8-36 dplyr_0.4.2 classInt_0.1-22
## [10] ggplot2_1.0.1 rgdal_1.0-4 sp_1.1-1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.11.6 RColorBrewer_1.1-2 formatR_1.2
## [4] plyr_1.8.3 class_7.3-13 tools_3.2.1
## [7] digest_0.6.8 evaluate_0.7 gtable_0.1.2
## [10] lattice_0.20-33 DBI_0.3.1 yaml_2.1.13
## [13] parallel_3.2.1 proto_0.3-10 e1071_1.6-4
## [16] stringr_1.0.0 knitr_1.10.5 grid_3.2.1
## [19] R6_2.1.0 foreign_0.8-65 rmarkdown_0.7
## [22] reshape2_1.4.1 magrittr_1.5 scales_0.2.5
## [25] htmltools_0.2.6 MASS_7.3-42 assertthat_0.1
## [28] colorspace_1.2-6 labeling_0.3 stringi_0.5-5
## [31] lazyeval_0.1.10 munsell_0.4.2
The next installment, to be announced by @technocrat on twitter, will illustrate dangers in using counts in comparing states with greatly differing populations.
I am unable to answer questions relating to the use of this code under Windows.
See sources in the walkthrough installment.
Copyright (c) 2015, Richard Careaga
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This could be done on demand within R; however, the data does not change frequently (annual editions). There are three versions of the cartographic boundary file. cb_2014_us_state_5m.zip is intermediate in resolution; cb_2014_us_state_500K is unsatisfactorily low-resolution; cb_2014_us_state_20m.zip is unnecessarily high-resolution for displaying thematic data.↩